package com.dyness.poi.append;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.dyness.poi.controller.ExcelLoadUtil;
import com.dyness.poi.level.CityExtend;
import com.dyness.sdsp.domain.SdspCity;
import com.dyness.sdsp.domain.SysMessage;
import com.dyness.sdsp.domain.SysMessageHelp;
import com.dyness.sdsp.service.SdspCityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author haiqing.gao
 * @date 2024-01-05 15:00
 */
@Service
public class AppendCityService {
    @Autowired
    private SdspCityService sdspCityService;


    public List<AppendCityExtend> loadCityExtend(List<CityExtend> levelData) {
        List<CityExtend> cityExtends = levelData.stream().filter(city -> city.getPid() != 0).collect(Collectors.toList());
        String file = ResourceUtil.getResource("city_4.xlsx").getFile();
        // String address = "C:\\Users\\gaohaiqing\\Downloads\\city_4.xlsx";
        List<AppendCity> appendCities = ExcelLoadUtil.loadData(file, AppendCity.class)
                .stream().filter(city -> org.apache.commons.lang3.StringUtils.isNotEmpty(city.getCode()))
                .collect(Collectors.toList());
        System.out.println("appendCities = " + appendCities);

        List<AppendCityExtend> appendCityExtendList = appendCities.stream().filter(c -> !StrUtil.isBlank(c.getDistrict())).map(city -> {
            AppendCityExtend appendCityExtend = new AppendCityExtend();
            BeanUtil.copyProperties(city, appendCityExtend);
            appendCityExtend.setId(IdUtil.getSnowflakeNextId());
            Long pid = cityExtends.stream().filter(district ->
                    (district.getName().equalsIgnoreCase(city.getDistrict()) || district.getGerman().equalsIgnoreCase(city.getDistrict())
                            && (district.getState().equalsIgnoreCase(city.getState()))
                    )
            ).findFirst().map(district -> district.getId()).orElse(0L);
            appendCityExtend.setPid(pid);
            return appendCityExtend;
        }).collect(Collectors.toList());

        // 校验
        List<AppendCity> errorAppendCities = appendCityExtendList.stream()
                .filter(appendCity -> appendCity.getPid() == 0).collect(Collectors.toList());
        // .forEach(originCityExtend -> System.out.println("原有城市出错的有originCityExtend = " + originCityExtend));
        if (CollectionUtil.isNotEmpty(errorAppendCities)) {
            System.out.println("补充数据: ============================");
        }
        // for (int i = 0; i < errorOriginCities.size(); i++) {
        //     System.out.print("A2=\"" + errorOriginCities.get(i).getCode() + "\",");
        // }
        errorAppendCities.forEach(originCityExtend -> System.out.println(originCityExtend.getCode()));

        List<AppendCityExtend> correctAppendCity = appendCityExtendList.stream().filter(appendCity -> appendCity.getPid() != 0).collect(Collectors.toList());

        return correctAppendCity;
    }

    public void saveLevelData(List<AppendCityExtend> list) {
        List<SysMessage> sysMessageList = new ArrayList<>();
        List<SdspCity> sdspCityList = new ArrayList<>();
        list.forEach(data -> {
            SdspCity cityPO = new SdspCity();
            BeanUtils.copyProperties(data, cityPO);
            cityPO.setCategory("city");
            cityPO.setCreatedBy("2");
            cityPO.setCreatedTime(new Date());
            cityPO.setModifiedBy("2");
            cityPO.setModifiedTime(new Date());
            if (StringUtils.hasText(data.getLatitude())) {
                cityPO.setLat(Double.parseDouble(data.getLatitude()));
            }
            if (StringUtils.hasText(data.getLongitude())) {
                cityPO.setLon(Double.parseDouble(data.getLongitude()));
            }
            sdspCityList.add(cityPO);

            // 构造message
            String code = data.getCode();
            String chinese = data.getChinese();
            String english = data.getEnglish();
            String german = data.getGerman();
            SysMessage temp1 = SysMessageHelp.buildZh(code, chinese);
            SysMessage temp2 = SysMessageHelp.buildEn(code, english);
            SysMessage temp3 = SysMessageHelp.buildDe(code, german);
            sysMessageList.add(temp1);
            sysMessageList.add(temp2);
            sysMessageList.add(temp3);
        });

        sdspCityService.saveData(sysMessageList, sdspCityList);
    }
}
